//
//  YTDetailNewController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/2.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTDetailNewController.h"
#import "YTShareViewController.h"
#define kDetailNewUrl @"Home/getLiveInfo"
@interface YTDetailNewController ()
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property(nonatomic ,strong)YTLivesModel *model;
@property (weak, nonatomic) IBOutlet UIButton *goodButton;
@property (weak, nonatomic) IBOutlet UIButton *badButton;

@property(nonatomic ,strong)UIButton *currentBtn;
@end

@implementation YTDetailNewController

- (void)setModel:(YTLivesModel *)model
{
    _model = model;
    
    self.timeLabel.text = model.created_at;
    self.titleLabel.text = model.post_title;
    self.contentLabel.text = model.content;
    self.dateLabel.text = model.post_date;
    
    self.tableView.rowHeight = [self.model detailHeight];
    
    if (model.is_like.boolValue) {
        
        self.goodButton.selected = YES;
        self.currentBtn = self.goodButton;
        [self.goodButton setTitle:[NSString stringWithFormat:@"利好 %@",model.up_counts] forState:UIControlStateSelected];
    }else{
        self.goodButton.selected = NO;
        [self.goodButton setTitle:[NSString stringWithFormat:@"利好 %@",model.up_counts] forState:UIControlStateNormal];
    }
    
    // 利空
    if (model.no_like.boolValue) {
        
        self.badButton.selected = YES;
        self.currentBtn = self.badButton;
        [self.badButton setTitle:[NSString stringWithFormat:@"利空 %@",model.down_counts] forState:UIControlStateSelected];
    }else{
        self.badButton.selected = NO;
        [self.badButton setTitle:[NSString stringWithFormat:@"利空 %@",model.down_counts] forState:UIControlStateNormal];
    }
    
    [self.tableView reloadData];

}

- (IBAction)shareButtonClick:(id)sender
{
    YTShareViewController *shareVC = [[YTShareViewController alloc] init];
    shareVC.model = self.model;
    [self presentViewController:shareVC animated:true completion:nil];
}

- (IBAction)badOrGoodBtnClick:(UIButton *)sender
{
    if (!YTNetworkManager.shareManager.account.auth) {
        
        YTLoginViewController *logVC = [[YTLoginViewController alloc] init];
        [self.navigationController pushViewController:logVC animated:true];
        return;
    }
    
    NSString *type = (sender.tag == 1001) ? @"0" : @"1";
    
    [YTNetworkManager.shareManager postNewFlashGoodOrBadWithlid:self.ID
                                                           type:type
                                                     completion:^(BOOL isSucc, NSString *statusInfo)
     {
         if (!isSucc) {
             [self.view showTipWithTipStr:statusInfo];
             return ;
         }
         
         [self.view showAwardWithTipStr:statusInfo ? [NSString stringWithFormat:@"投票成功 +%@ 算力",statusInfo] : @"投票成功"];
         self.currentBtn.selected = NO;
         sender.selected = YES;
         self.currentBtn = sender;
         
         [self loadData];
         
     }];
}

// 实例化
+ (instancetype)controller
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YTDetailNewController" bundle:nil];
    YTDetailNewController *vc = [sb instantiateViewControllerWithIdentifier:@"DetailNewSBID"];
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadData];
    
    self.view.backgroundColor = [UIColor colorWithRed:224.0f/250.0f
                                                green:227.0f/250.0f
                                                 blue:228.0f/250.0f alpha:1];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(backButtonClick)];
    
    self.navigationItem.title = @"快讯详情";
}

- (void)backButtonClick
{
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {
        [self dismissViewControllerAnimated:true completion:nil];
        [pushJudge setObject:@""forKey:@"push"];
    }else{
        [self.navigationController popViewControllerAnimated:true];
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.model detailHeight];
}

- (void)loadData
{
    [YTNetworkManager.shareManager requestWithMethod:MethodGet
                                           UrlString:kDetailNewUrl
                                              params:@{@"post_id":self.ID}
                                          completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            [self.view showTipWithTipStr:@"网络不给力..."];
            return ;
        }
        
        if ([json[@"code"] intValue] == 1) {
            self.model = [YTLivesModel yy_modelWithJSON:json[@"data"]];
            
           
        }else{
            [self.view showTipWithTipStr:json[@"msg"]];
        }
    }];
}

@end
